package GUI;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import RnD.SuperCatcher;
import Rout.UserAgg;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

/**
 *
 * @author user
 */
public class GUI_Dsk extends javax.swing.JFrame {

    private RnD.SuperCatcher test;
    private ArrayList<String> urls;
    private ArrayList<Integer> agents, cloneAgents;

    private final PrintStream standardOut;
    private final PrintStream printOutStream, printErrStream;
    private RnD.SystemDetecter OsName;
    private String driverLocation, options;
    private Runnable runDriver;
    private Thread myThread;
    private UserAgg myAg;
    
    private JFileChooser fileChooser;
    

    /**
     * Creates new form NewJFrame
     */
    public GUI_Dsk() {

        myAg = new UserAgg();
        
        urls = new ArrayList<String>();
        agents = new ArrayList<Integer>();
         cloneAgents = new ArrayList<Integer>();
        
        
        initComponents();
        
        printOutStream = new PrintStream(new CustomOutputStream(Output));
        printErrStream = new PrintStream(new CustomOutputStream(jTextArea1));

        OsName = new RnD.SystemDetecter();

        standardOut = System.out;
        System.setOut(printOutStream);
        System.setErr(printErrStream);
        
                                if (RemoteIPCH.isSelected()) {
            setDriverLocation(getRemoteIP());
            System.out.println("Remote IP: " + getRemoteIP());
        } else {
            setDriverLocation(getOSDriver());
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jMenuItem1 = new javax.swing.JMenuItem();
        jFileChooser1 = new javax.swing.JFileChooser();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        plusURL = new javax.swing.JButton();
        AddURL = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        XPbutton = new javax.swing.JTextField();
        jCheckBoxLocator = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckClickOnmilk = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jSlider1 = new javax.swing.JSlider();
        jLabel3 = new javax.swing.JLabel();
        Start = new javax.swing.JButton();
        Stop = new javax.swing.JButton();
        Retry = new javax.swing.JButton();
        Error = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        Output = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        Browsers = new javax.swing.JMenu();
        jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem2 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem3 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem4 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem5 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem6 = new javax.swing.JCheckBoxMenuItem();
        jCheckBoxMenuItem7 = new javax.swing.JCheckBoxMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        RemoteIPCH = new javax.swing.JCheckBoxMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        plusURL.setText("+");
        plusURL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                plusURLActionPerformed(evt);
            }
        });

        AddURL.setBackground(new java.awt.Color(238, 238, 238));
        AddURL.setText("http://www.4shared.com/photo/jIP-_j8sce/Test_file.html");
        AddURL.setBorder(javax.swing.BorderFactory.createTitledBorder("Ad URL"));
        AddURL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddURLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(AddURL, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(plusURL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(AddURL)
                    .addComponent(plusURL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        XPbutton.setBackground(new java.awt.Color(238, 238, 238));
        XPbutton.setText("//*[@id=\"doSearch\"]");
        XPbutton.setBorder(javax.swing.BorderFactory.createTitledBorder("Button locator (xpath)"));

        jCheckBoxLocator.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxLocatorActionPerformed(evt);
            }
        });

        jCheckClickOnmilk.setText("ClickOnMilk (popunder test)");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(XPbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBoxLocator))
                    .addComponent(jCheckClickOnmilk))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(XPbutton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jCheckBoxLocator)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckClickOnmilk)
                .addContainerGap())
        );

        jSlider1.setMajorTickSpacing(1);
        jSlider1.setMaximum(10);
        jSlider1.setMinimum(1);
        jSlider1.setMinorTickSpacing(2);
        jSlider1.setPaintLabels(true);
        jSlider1.setBorder(javax.swing.BorderFactory.createTitledBorder("attempts to try"));
        jSlider1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}"), jSlider1, org.jdesktop.beansbinding.BeanProperty.create("value"), "sliderValue");
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}"), jSlider1, org.jdesktop.beansbinding.BeanProperty.create("paintTicks"));
        bindingGroup.addBinding(binding);

        jSlider1.putClientProperty("JComponent.sizeVariant", "small");
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });
        jSlider1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jSlider1PropertyChange(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jSlider1, org.jdesktop.beansbinding.ELProperty.create("${value}"), jLabel3, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        Start.setText("Start");
        Start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartActionPerformed(evt);
            }
        });

        Stop.setText("Stop");
        Stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StopActionPerformed(evt);
            }
        });

        Retry.setText("Retry");
        Retry.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RetryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 32, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(Start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Stop, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Retry, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Start, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Stop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Retry, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        Output.setEditable(false);
        Output.setBackground(new java.awt.Color(0, 0, 0));
        Output.setColumns(20);
        Output.setForeground(new java.awt.Color(255, 255, 51));
        Output.setRows(5);
        Output.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        Output.setCaretColor(new java.awt.Color(255, 255, 0));
        Output.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        Output.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        Output.setName("consoleOutput"); // NOI18N
        Output.setSelectedTextColor(new java.awt.Color(204, 204, 0));
        Output.setSelectionColor(new java.awt.Color(204, 255, 0));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, Output, org.jdesktop.beansbinding.ELProperty.create("${text}"), Output, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(Output);

        Error.addTab("Out", jScrollPane1);

        jTextArea1.setBackground(java.awt.Color.black);
        jTextArea1.setColumns(20);
        jTextArea1.setForeground(new java.awt.Color(255, 255, 51));
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        Error.addTab("Err", jScrollPane2);

        jMenu1.setText("File");

        jMenuItem2.setText("quit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        Browsers.setText("Browsers");
        Browsers.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        Browsers.setRequestFocusEnabled(false);
        Browsers.setRolloverEnabled(true);
        Browsers.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                BrowsersComponentAdded(evt);
            }
        });
        Browsers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowsersActionPerformed(evt);
            }
        });

        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("Chrome_win");
        jCheckBoxMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxMenuItem1ActionPerformed(evt);
            }
        });
        Browsers.add(jCheckBoxMenuItem1);

        jCheckBoxMenuItem2.setText("Firefox_30");
        jCheckBoxMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxMenuItem2ActionPerformed(evt);
            }
        });
        Browsers.add(jCheckBoxMenuItem2);

        jCheckBoxMenuItem3.setLabel("Chrome_38_Android");
        Browsers.add(jCheckBoxMenuItem3);

        jCheckBoxMenuItem4.setText("IE_10");
        Browsers.add(jCheckBoxMenuItem4);

        jCheckBoxMenuItem5.setText("Safari_7");
        Browsers.add(jCheckBoxMenuItem5);

        jCheckBoxMenuItem6.setText("Safari_8_mobile");
        Browsers.add(jCheckBoxMenuItem6);

        jCheckBoxMenuItem7.setText("Chrome_linux");
        Browsers.add(jCheckBoxMenuItem7);

        jMenuBar1.add(Browsers);

        jMenu2.setText("Edit");

        jMenuItem3.setText("driver patch (custom)");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuItem4.setText("Clear ad URLs");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        RemoteIPCH.setText("Remote IP");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, new javax.swing.JOptionPane(), org.jdesktop.beansbinding.ELProperty.create("${enabled}"), RemoteIPCH, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        binding.setSourceNullValue(true);
        binding.setSourceUnreadableValue(true);
        bindingGroup.addBinding(binding);

        RemoteIPCH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoteIPCHActionPerformed(evt);
            }
        });
        jMenu2.add(RemoteIPCH);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(6, 6, 6)
                .addComponent(Error, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(Error))
                        .addContainerGap())))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged


    }//GEN-LAST:event_jSlider1StateChanged

    private void jSlider1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSlider1PropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jSlider1PropertyChange

    private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed

        if (jCheckBoxMenuItem1.isSelected()) {
            if (!agents.contains(1)) {
                agents.add(1);
            }            
        }

        if (jCheckBoxMenuItem2.isSelected()) {
            if (!agents.contains(2)) {
                agents.add(2);
            } 
        }

        if (jCheckBoxMenuItem3.isSelected()) {
            if (!agents.contains(3)) {
                agents.add(3);
            } 
        }
        if (jCheckBoxMenuItem4.isSelected()) {
            if (!agents.contains(4)) {
                agents.add(4);
            } 
        }
        if (jCheckBoxMenuItem5.isSelected()) {
            if (!agents.contains(5)) {
                agents.add(5);
            } 
        }
        if (jCheckBoxMenuItem6.isSelected()) {
            if (!agents.contains(6)) {
                agents.add(6);
            } 
        }
        if (jCheckBoxMenuItem7.isSelected()) {
            if (!agents.contains(7)) {
                agents.add(7);
            } 
        }       

        if (jCheckBoxLocator.isSelected()) {
            setOptions(getXPlocator());
        } else { 
            setOptions(null);
        }
        
        
        if (!agents.isEmpty() && !urls.isEmpty()) {
                 for (final int agent : agents) {
                     new Thread(new Procedure(agent)).start();
                     Thread.yield();
        }  
        } else {
            if (urls.isEmpty()) {
                JOptionPane.showMessageDialog(AddURL, "Please choose some browsers to start the test");
            } else if (!agents.isEmpty()) {
              JOptionPane.showMessageDialog(AddURL, "Please choose some browsers to start the test");  
            }            
        }
 
       
        cloneAgents = agents;
        System.err.println("TEST!!!!!!!!!!!!!!!!!!!!!!!");
        agents.clear();                     
       
    }//GEN-LAST:event_StartActionPerformed

    private void StopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopActionPerformed
        try {
//            Thread.sleep(1000);
//            test.ForceDown();
            myThread.interrupt();
            System.out.println("===============\nTEST STOPED\n===============\nStatus: " + myThread.getState());
//        } catch (InterruptedException ex) {
//            Logger.getLogger(GUI_Dsk.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(GUI_Dsk.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_StopActionPerformed

    private void AddURLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddURLActionPerformed


    }//GEN-LAST:event_AddURLActionPerformed

    private void plusURLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_plusURLActionPerformed

        if (!urls.contains(getServURL())) {
            urls.add(getServURL());
            showURLs();
        }

//        jPanel1.updateUI();
    }//GEN-LAST:event_plusURLActionPerformed

    private void jCheckBoxLocatorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxLocatorActionPerformed
        if (jCheckBoxLocator.isSelected()) {
            System.out.println("Add test step\n click on element: " + getXPlocator());
        }
    }//GEN-LAST:event_jCheckBoxLocatorActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed

        urls.clear();
        System.out.println("List URL cleared:\n" + urls);
        showURLs();
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void RetryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RetryActionPerformed
        try {
            
            System.out.println("===============\nTEST RETRY\n===============\nStatus: " + myThread.getState());
            for (int agent : cloneAgents) {
            runDriverJob(agent);
        }
        }
        catch (Exception ex) {
            Logger.getLogger(GUI_Dsk.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_RetryActionPerformed

    private void jCheckBoxMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem2ActionPerformed

    }//GEN-LAST:event_jCheckBoxMenuItem2ActionPerformed

    private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
           
    }//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed

    private void BrowsersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowsersActionPerformed
        for (int agents : agents) {
            System.out.println(agents + ". "+myAg.getAgentdescr(agents));
        }       
    }//GEN-LAST:event_BrowsersActionPerformed

    private void BrowsersComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_BrowsersComponentAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_BrowsersComponentAdded

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
       fileChooser = new JFileChooser();
       fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
       int result = fileChooser.showOpenDialog(Browsers);
       if (result == JFileChooser.APPROVE_OPTION) {
           File selectedFile = fileChooser.getSelectedFile();
           System.out.println("Selected file: " + selectedFile.getAbsolutePath());
}
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void RemoteIPCHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoteIPCHActionPerformed
        final JOptionPane oo = new JOptionPane();
                oo.showInputDialog(rootPane, "Enter remote IP adress", options, 1);                
                oo.setToolTipText("Don't forget to run remote server before start testing");
                setDriverLocation(oo.getInputValue().toString());
                System.out.println(getDriverLocation());
    }//GEN-LAST:event_RemoteIPCHActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    public static void build() {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI_Dsk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI_Dsk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI_Dsk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI_Dsk.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new GUI_Dsk().setVisible(true);
            }
        });
    }

    public int getSliderValue() {
        return jSlider1.getValue();
    }

    public String getServURL() {
        return AddURL.getText();
    }

    public String getRemoteIP() {
        JOptionPane hh = new JOptionPane(OsName, 2);
        hh.setVisible(true);
        return hh.getInputValue().toString();
    }

    public String getXPlocator() {
        if (jCheckBoxLocator.isSelected()) {
            return XPbutton.getText();
        } else return "";
        
    }

    private String getOSDriver() {

        RnD.SystemDetecter detec = new RnD.SystemDetecter();
        if (detec.detectSystem().contains("nux")) {
            setDriverLocation("EXEs/NIX/chromedriver");

        }
        if (detec.detectSystem().contains("mac")) {
            setDriverLocation("EXEs/OSX/chromedriver");
        }
        if (detec.detectSystem().contains("indow")) {
            setDriverLocation("EXEs/WIN/chromedriver.exe");
        }
        System.out.println("System type: " + detec.detectSystem() + "\nDriver path: " + getDriverLocation());
        return driverLocation;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField AddURL;
    private javax.swing.JMenu Browsers;
    private javax.swing.JTabbedPane Error;
    private javax.swing.JTextArea Output;
    private javax.swing.JCheckBoxMenuItem RemoteIPCH;
    private javax.swing.JButton Retry;
    private javax.swing.JButton Start;
    private javax.swing.JButton Stop;
    private javax.swing.JTextField XPbutton;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBoxLocator;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem2;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem3;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem4;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem5;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem6;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem7;
    private javax.swing.JCheckBox jCheckClickOnmilk;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JButton plusURL;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {

        GUI_Dsk.build();
    }

    public String getDriverLocation() {
        return driverLocation;
    }

    public void setDriverLocation(String driverLocation) {
        this.driverLocation = driverLocation;
    }

    public String getOptions() {
        return options;
    }

    public void setOptions(String options) {
        this.options = options;
    }

    public void addtestEnv(int i) {
        agents.add(i);
        System.out.println("test: " + myAg.getAgentdescr(i));
    }

    public void showURLs() {
        System.out.println("URLs:");
        for (String url : urls) {
            System.out.println(urls.indexOf(url) + ": " + url.intern());
        }
    }
    
    public void runDriverJob(final int agent) {
//                for (final int agentss: agents) {
            
            System.out.println(urls + getXPlocator() + agent + jSlider1.getValue() + jCheckClickOnmilk.isSelected());
            
            runDriver = new Runnable() {
                public void run() {
                    
                    try {                        
                        test.setUp_via_API(getOSDriver(), urls, getXPlocator(), agent, jSlider1.getValue(), jCheckClickOnmilk.isSelected());                        
                        test.Test();
                        test.Down();
                    } catch (Exception ex) {
                        Logger.getLogger(GUI_Dsk.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            };
        myThread = new Thread(runDriver);
        myThread.setName(myAg.getAgentdescr(agent));
        System.out.println("\nTest: " + myThread.getName() + "  ------ started");
        System.out.println(Thread.currentThread().getName()); 
        myThread.start();
//        }
    }
      

class Procedure implements Runnable {
    
    int age;
    
    public Procedure (int age) {
        this.age = age;
    }
    
    @Override
    public void run() {                
            System.out.println(Thread.currentThread().getName()+">> " + myAg.getAgentdescr(age));
        
        try {
            new SuperCatcher().setUp_via_API(getOSDriver(), urls, getXPlocator(), age, jSlider1.getValue(), jCheckClickOnmilk.isSelected());
            
        } catch (Exception ex) {
            Logger.getLogger(GUI_Dsk.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
    }
}